package fr.iut2.sil4.data;

import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Types;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.torque.NoRowsException;
import org.apache.torque.OptimisticLockingFailedException;
import org.apache.torque.TooManyRowsException;
import org.apache.torque.Torque;
import org.apache.torque.TorqueException;
import org.apache.torque.TorqueRuntimeException;
import org.apache.torque.criteria.Criteria;
import org.apache.torque.criteria.Criterion;
import org.apache.torque.om.mapper.RecordMapper;
import org.apache.torque.om.mapper.CompositeMapper;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.ComboKey;
import org.apache.torque.map.TableMap;
import org.apache.torque.util.Transaction;
import org.apache.torque.util.ColumnValues;
import org.apache.torque.util.JdbcTypedValue;



/**
 * Cours Table
 *
 * The skeleton for this class was autogenerated by Torque on:
 *
 * [Mon Jan 20 15:56:23 CET 2014]
 *
 * You should not use this class directly.  It should not even be
 * extended; all references should be to CoursPeer
 */

public abstract class BaseCoursPeerImpl
    extends org.apache.torque.util.BasePeerImpl<Cours>
{
    /** The class log. */
    private static Log log = LogFactory.getLog(BaseCoursPeerImpl.class);

    /** Serial version */
    private static final long serialVersionUID = 1390229783986L;



    /**
     * Constructor.
     * The recordMapper, tableMap and databaseName fields are correctly
     * initialized.
     */
    public BaseCoursPeerImpl()
    {
        this(new CoursRecordMapper(),
            CoursPeer.TABLE,
            CoursPeer.DATABASE_NAME);
    }

    /**
     * Constructor providing the objects to be injected as parameters.
     *
     * @param recordMapper a record mapper to map JDBC result sets to objects
     * @param tableMap the default table map
     * @param databaseName the name of the database
     */
    public BaseCoursPeerImpl(
            RecordMapper<Cours> recordMapper, 
            TableMap tableMap,
            String databaseName)
    {
        super(recordMapper, tableMap, databaseName);
    }


    /**
     * Selects Cours objects from the database which have
     * the same content as the passed object.
     *
     * @return The list of selected objects, not null.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public List<Cours> doSelect(Cours obj)
            throws TorqueException
    {
        return doSelect(buildSelectCriteria(obj));
    }

    /**
     * Selects at most one Cours object from the database
     * which has the same content as the passed object.
     *
     * @return the selected Object, or null if no object was selected.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public Cours doSelectSingleRecord(
                Cours obj)
            throws TorqueException
    {
        List<Cours> coursList = doSelect(obj);
        Cours cours = null;
        if (coursList.size() > 1)
        {
            throw new TooManyRowsException("Object " + obj 
                + " matched more than one record");
        }
        if (!coursList.isEmpty())
        {
            cours = coursList.get(0);
        }
        return cours;
    }

    /**
     * Returns a new instance of the Data object class
     */
    public Cours getDbObjectInstance()
    {
        return new Cours();
    }


    /**
     * Method to do inserts.
     *
     * @param columnValues the values to insert.
     *
     * @return the primary key of the inserted row.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public ObjectKey doInsert(ColumnValues columnValues) throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(
                    CoursPeer.DATABASE_NAME);
            ObjectKey result = doInsert(columnValues, connection);
            Transaction.commit(connection);
            connection = null;
            return result;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * Method to do inserts.  This method is to be used during a transaction,
     * otherwise use the doInsert(Criteria) method.
     *
     * @param columnValues the values to insert.
     * @param con the connection to use, not null.
     *
     * @return the primary key of the inserted row.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public ObjectKey doInsert(ColumnValues columnValues, Connection con)
        throws TorqueException
    {
        correctBooleans(columnValues);
        return super.doInsert(columnValues, con);
    }

    /**
     * Method to do inserts
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public void doInsert(Cours obj) throws TorqueException
    {
        obj.setPrimaryKey(doInsert(buildColumnValues(obj)));
        obj.setNew(false);
        obj.setModified(false);
    }

    /**
     * Method to do inserts.  This method is to be used during a transaction,
     * otherwise use the doInsert(Cours) method.  It will take
     * care of the connection details internally.
     *
     * @param obj the data object to insert into the database.
     * @param con the connection to use
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public void doInsert(Cours obj, Connection con)
        throws TorqueException
    {
        ObjectKey primaryKey = doInsert(buildColumnValues(obj), con);
        if (primaryKey != null)
        {
            obj.setPrimaryKey(primaryKey);
        }
        obj.setNew(false);
        obj.setModified(false);
    }

    /**
     * Method to do updates.
     *
     * @param columnValues the values to update plus the primary key
     *        identifying the row to update.
     *
     * @return the number of affected rows.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public int doUpdate(ColumnValues columnValues) throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(
                    CoursPeer.DATABASE_NAME);
            int result = doUpdate(columnValues, connection);
            Transaction.commit(connection);
            connection = null;
            return result;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * Method to do updates.  This method is to be used during a transaction,
     * otherwise use the doUpdate(Criteria) method.
     *
     * @param columnValues the values to update plus the primary key
     *        identifying the row to update.
     * @param con the connection to use, not null.
     *
     * @return the number of affected rows.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public int doUpdate(ColumnValues columnValues, Connection con)
        throws TorqueException
    {
        Criteria selectCriteria 
                = new Criteria(CoursPeer.DATABASE_NAME);
        correctBooleans(columnValues);

        selectCriteria.where(
                CoursPeer.COURS_ID,
                columnValues.remove(CoursPeer.COURS_ID).getValue());


        int rowCount = doUpdate(selectCriteria, columnValues, con);
        return rowCount;
    }

    /**
     * Updates an Cours in the database.
     * The primary key is used to identify the object to update.
     *
     * @param obj the data object to update in the database.
     *
     * @return the number of affected rows.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public int doUpdate(Cours obj) throws TorqueException
    {
        ColumnValues columnValues = buildColumnValues(obj);
        int result = doUpdate(columnValues);
        obj.setModified(false);
        return result;
    }

    /**
     * Updates a Cours in the database.
     * The primary key is used to identify the object to update.
     * This method is to be used during a transaction,
     * otherwise the doUpdate(Cours) method can be used.
     *
     * @param obj the data object to update in the database.
     * @param con the connection to use, not null.
     
     * @return the number of affected rows.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public int doUpdate(Cours obj, Connection con)
        throws TorqueException
    {
        ColumnValues columnValues = buildColumnValues(obj);
        int result = doUpdate(columnValues, con);
        obj.setModified(false);
        return result;
    }

    /**
     * Deletes a data object, i.e. a row in a table, in the database.
     *
     * @param obj the data object to delete in the database, not null.
     *
     * @return the number of deleted rows.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public int doDelete(Cours obj) throws TorqueException
    {
        int result = doDelete(buildCriteria(obj.getPrimaryKey()));
        obj.setDeleted(true);
        return result;
    }

    /**
     * Deletes a data object, i.e. a row in a table, in the database.
     * This method is to be used during a transaction, otherwise use the
     * doDelete(Cours) method.
     *
     * @param obj the data object to delete in the database, not null.
     * @param con the connection to use, not null.
     *
     * @return the number of deleted rows.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public int doDelete(Cours obj, Connection con)
        throws TorqueException
    {
        int result = doDelete(buildCriteria(obj.getPrimaryKey()), con);
        obj.setDeleted(true);
        return result;
    }

    /**
     * Deletes data objects, i.e. rows in a table, in the database.
     *
     * @param objects the data object to delete in the database, not null,
     *        may not contain null.
     *
     * @return the number of deleted rows.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public int doDelete(Collection<Cours> objects)
            throws TorqueException
    {
        int result = doDelete(buildPkCriteria(objects));
        for (Cours object : objects)
        {
            object.setDeleted(true);
        }
        return result;
    }

    /**
     * Deletes data objects, i.e. rows in a table, in the database.
     * This method uses the passed connection to delete the rows;
     * if a transaction is open in the connection, the deletion happens inside
     * this transaction.
     *
     * @param objects the data objects to delete in the database, not null,
     *        may not contain null.
     * @param con the connection to use for deleting, not null.
     *
     * @return the number of deleted rows.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public int doDelete(
            Collection<Cours> objects,
            Connection con)
        throws TorqueException
    {
        int result = doDelete(buildPkCriteria(objects), con);
        for (Cours object : objects)
        {
            object.setDeleted(true);
        }
        return result;
    }

    /**
     * Deletes a row in the database.
     *
     * @param pk the ObjectKey that identifies the row to delete.
     *
     * @return the number of deleted rows.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public int doDelete(ObjectKey pk) throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(
                    CoursPeer.DATABASE_NAME);
            int deletedRows = doDelete(pk, connection);
            Transaction.commit(connection);
            connection = null;
            return deletedRows;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * Deletes a row in the database.
     * This method is to be used during a transaction,
     * otherwise use the doDelete(ObjectKey) method.
     *
     * @param pk the ObjectKey that identifies the row to delete.
     * @param con the connection to use for deleting, not null.
     *
     * @return the number of deleted rows.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public int doDelete(ObjectKey pk, Connection con)
        throws TorqueException
    {
        return doDelete(buildCriteria(pk), con);
    }

    /** 
     * Build a Criteria object which selects all objects which have a given
     * primary key.
     *
     * @param pk the primary key value to build the criteria from, not null.
     */
    public Criteria buildCriteria(ObjectKey pk)
    {
        Criteria criteria = new Criteria();
        criteria.and(CoursPeer.COURS_ID, pk);
        return criteria;
     }

    /** 
     * Build a Criteria object which selects all objects which primary keys
     * are contained in the passed collection.
     *
     * @param pks the primary key values to build the criteria from, not null,
     *        may not contain null.
     */
    public Criteria buildCriteria(Collection<ObjectKey> pks)
    {
        Criteria criteria = new Criteria();
        criteria.andIn(CoursPeer.COURS_ID, pks);
        return criteria;
     }


    /** 
     * Build a Criteria object which selects all passed objects using their
     * primary key. Objects which do not yet have a primary key are ignored.
     *
     * @param objects the objects to build the criteria from, not null,
     *        may not contain null.
     */
    public Criteria buildPkCriteria(
            Collection<Cours> objects)
    {
        List<ObjectKey> pks = new ArrayList<ObjectKey>(objects.size());
        for (Cours object : objects)
        {
            ObjectKey pk = object.getPrimaryKey();
            if (pk != null)
            {
                pks.add(pk);
            }
        }
        return buildCriteria(pks);
    }

    /** 
     * Build a Criteria object from the data object for this peer.
     * The primary key columns are only added if the object is not new.
     *
     * @param obj the object to build the criteria from, not null.
     */
    public Criteria buildCriteria(Cours obj)
    {
        Criteria criteria = new Criteria(CoursPeer.DATABASE_NAME);
        if (!obj.isNew())
        {
            criteria.and(CoursPeer.COURS_ID, obj.getCoursId());
        }
        criteria.and(CoursPeer.START_TIME, obj.getStartTime());
        criteria.and(CoursPeer.END_TIME, obj.getEndTime());
        criteria.and(CoursPeer.SALLE, obj.getSalle());
        criteria.and(CoursPeer.MATIERE_ID, obj.getMatiereId());
        criteria.and(CoursPeer.ENSEIGNANT_ID, obj.getEnseignantId());
        criteria.and(CoursPeer.GROUPE_ID, obj.getGroupeId());
        return criteria;
    }

    /** 
     * Build a Criteria object from the data object for this peer,
     * skipping all binary columns.
     *
     * @param obj the object to build the criteria from, not null.
     */
    public Criteria buildSelectCriteria(Cours obj)
    {
        Criteria criteria = new Criteria(CoursPeer.DATABASE_NAME);
        if (!obj.isNew())
        {
            criteria.and(CoursPeer.COURS_ID, obj.getCoursId());
        }
        criteria.and(CoursPeer.START_TIME, obj.getStartTime());
        criteria.and(CoursPeer.END_TIME, obj.getEndTime());
        criteria.and(CoursPeer.SALLE, obj.getSalle());
        criteria.and(CoursPeer.MATIERE_ID, obj.getMatiereId());
        criteria.and(CoursPeer.ENSEIGNANT_ID, obj.getEnseignantId());
        criteria.and(CoursPeer.GROUPE_ID, obj.getGroupeId());
        return criteria;
    }

    /** 
     * Returns the contents of the object as ColumnValues object.
     * Primary key columns which are generated on insertion are not
     * added to the returned object if they still have their initial
     * value. Also, columns which have the useDatabaseDefaultValue
     * flag set to true are also not added to the returned object
     * if they still have their initial value.
     *
     * @throws TorqueException if the table map cannot be retrieved
     *         (should not happen).
     */
    public ColumnValues buildColumnValues(Cours cours)
            throws TorqueException
    {
        ColumnValues columnValues = new ColumnValues();
        if (!cours.isNew() 
            || cours.getCoursId() != 0)
        {
            columnValues.put(
                    CoursPeer.COURS_ID,
                    new JdbcTypedValue(
                        cours.getCoursId(),
                        4));
        }
        java.util.Date startTime = cours.getStartTime();
        if (startTime != null)
        {
            startTime = new java.sql.Timestamp(startTime.getTime());
        }
        columnValues.put(
                CoursPeer.START_TIME,
                new JdbcTypedValue(startTime, Types.TIMESTAMP));
        java.util.Date endTime = cours.getEndTime();
        if (endTime != null)
        {
            endTime = new java.sql.Timestamp(endTime.getTime());
        }
        columnValues.put(
                CoursPeer.END_TIME,
                new JdbcTypedValue(endTime, Types.TIMESTAMP));
        columnValues.put(
                CoursPeer.SALLE,
                new JdbcTypedValue(
                    cours.getSalle(),
                    12));
        columnValues.put(
                CoursPeer.MATIERE_ID,
                new JdbcTypedValue(
                    cours.getMatiereId(),
                    4));
        columnValues.put(
                CoursPeer.ENSEIGNANT_ID,
                new JdbcTypedValue(
                    cours.getEnseignantId(),
                    4));
        columnValues.put(
                CoursPeer.GROUPE_ID,
                new JdbcTypedValue(
                    cours.getGroupeId(),
                    4));
        return columnValues;
    }

    /**
     * Retrieve a single object by pk
     *
     * @param pk the primary key
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     * @throws NoRowsException Primary key was not found in database.
     * @throws TooManyRowsException Primary key was not found in database.
     */
    public Cours retrieveByPK(int pk)
        throws TorqueException, NoRowsException, TooManyRowsException
    {
        return retrieveByPK(SimpleKey.keyFor(pk));
    }

    /**
     * Retrieve a single object by pk
     *
     * @param pk the primary key
     * @param con the connection to use
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     * @throws NoRowsException Primary key was not found in database.
     * @throws TooManyRowsException Primary key was not found in database.
     */
    public Cours retrieveByPK(int pk, Connection con)
        throws TorqueException, NoRowsException, TooManyRowsException
    {
        return retrieveByPK(SimpleKey.keyFor(pk), con);
    }
    
    
    

    /**
     * Retrieve a single object by pk
     *
     * @param pk the primary key
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     * @throws NoRowsException Primary key was not found in database.
     * @throws TooManyRowsException Primary key was not found in database.
     */
    public Cours retrieveByPK(ObjectKey pk)
        throws TorqueException, NoRowsException, TooManyRowsException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(CoursPeer.DATABASE_NAME);
            Cours result = retrieveByPK(pk, connection);
            Transaction.commit(connection);
            connection = null;
            return result;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * Retrieve a single object by pk
     *
     * @param pk the primary key
     * @param con the connection to use
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     * @throws NoRowsException Primary key was not found in database.
     * @throws TooManyRowsException Primary key was not found in database.
     */
    public Cours retrieveByPK(ObjectKey pk, Connection con)
        throws TorqueException, NoRowsException, TooManyRowsException
    {
        Criteria criteria = buildCriteria(pk);
        List<Cours> v = doSelect(criteria, con);
        if (v.size() == 0)
        {
            throw new NoRowsException("Failed to select a row.");
        }
        else if (v.size() > 1)
        {
            throw new TooManyRowsException("Failed to select only one row.");
        }
        else
        {
            return (Cours)v.get(0);
        }
    }


    /**
     * Retrieve a multiple objects by pk
     *
     * @param pks List of primary keys
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public List<Cours> retrieveByPKs(Collection<ObjectKey> pks)
        throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(CoursPeer.DATABASE_NAME);
            List<Cours> result = retrieveByPKs(pks, connection);
            Transaction.commit(connection);
            connection = null;
            return result;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * Retrieve multiple objects by pk
     *
     * @param pks List of primary keys
     * @param dbcon the connection to use
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public List<Cours> retrieveByPKs(
                Collection<ObjectKey> pks,
                Connection dbcon)
            throws TorqueException
    {
        if (pks == null || pks.size() == 0)
        {
            return new ArrayList<Cours>();
        }
        Criteria criteria = buildCriteria(pks);
        List<Cours> result = doSelect(criteria, dbcon);
        return result;
    }


    /**
     * Selects a collection of dbObjectClassName objects pre-filled with their
     * Matiere objects.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public List<Cours> doSelectJoinMatiere(Criteria criteria)
        throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(
                    CoursPeer.DATABASE_NAME);
            List<Cours> result
                    = CoursPeer.doSelectJoinMatiere(criteria, connection);
            Transaction.commit(connection);
            connection = null;
            return result;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * Selects a collection of Cours objects pre-filled with their
     * Matiere objects.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public List<Cours> doSelectJoinMatiere(
            Criteria criteria, Connection conn)
        throws TorqueException
    {
        setDbName(criteria);

        addSelectColumns(criteria);
        fr.iut2.sil4.data.MatierePeer.addSelectColumns(criteria);

        CompositeMapper compositeMapper = new CompositeMapper();
        compositeMapper.addMapper(
                new CoursRecordMapper(),
                0);
        compositeMapper.addMapper(
                new MatiereRecordMapper(),
                CoursPeer.numColumns);
        criteria.addJoin(CoursPeer.MATIERE_ID,
            fr.iut2.sil4.data.MatierePeer.MATIERE_ID);

        correctBooleans(criteria);

        List<Cours> result = new ArrayList<Cours>();
        List<List<Object>> rawResult = doSelect(
                criteria, compositeMapper, conn);
        for (List<Object> rawResultRow : rawResult)
        {
            Cours obj1 = (Cours) rawResultRow.get(0);
            Matiere obj2 = (Matiere) rawResultRow.get(1);

            boolean newObject = true;
            for (int j = 0; j < result.size(); j++)
            {
                Cours temp_obj1 = result.get(j);
                fr.iut2.sil4.data.Matiere temp_obj2 = temp_obj1.getMatiere();
                if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey()))
                {
                    newObject = false;
                    temp_obj2.addCours(obj1);
                    break;
                }
            }
            if (newObject)
            {
                obj2.initCourss();
                obj2.addCours(obj1);
            }
            result.add(obj1);
        }
        return result;
    }
    /**
     * Selects a collection of dbObjectClassName objects pre-filled with their
     * Enseignant objects.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public List<Cours> doSelectJoinEnseignant(Criteria criteria)
        throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(
                    CoursPeer.DATABASE_NAME);
            List<Cours> result
                    = CoursPeer.doSelectJoinEnseignant(criteria, connection);
            Transaction.commit(connection);
            connection = null;
            return result;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * Selects a collection of Cours objects pre-filled with their
     * Enseignant objects.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public List<Cours> doSelectJoinEnseignant(
            Criteria criteria, Connection conn)
        throws TorqueException
    {
        setDbName(criteria);

        addSelectColumns(criteria);
        fr.iut2.sil4.data.EnseignantPeer.addSelectColumns(criteria);

        CompositeMapper compositeMapper = new CompositeMapper();
        compositeMapper.addMapper(
                new CoursRecordMapper(),
                0);
        compositeMapper.addMapper(
                new EnseignantRecordMapper(),
                CoursPeer.numColumns);
        criteria.addJoin(CoursPeer.ENSEIGNANT_ID,
            fr.iut2.sil4.data.EnseignantPeer.ENSEIGNANT_ID);

        correctBooleans(criteria);

        List<Cours> result = new ArrayList<Cours>();
        List<List<Object>> rawResult = doSelect(
                criteria, compositeMapper, conn);
        for (List<Object> rawResultRow : rawResult)
        {
            Cours obj1 = (Cours) rawResultRow.get(0);
            Enseignant obj2 = (Enseignant) rawResultRow.get(1);

            boolean newObject = true;
            for (int j = 0; j < result.size(); j++)
            {
                Cours temp_obj1 = result.get(j);
                fr.iut2.sil4.data.Enseignant temp_obj2 = temp_obj1.getEnseignant();
                if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey()))
                {
                    newObject = false;
                    temp_obj2.addCours(obj1);
                    break;
                }
            }
            if (newObject)
            {
                obj2.initCourss();
                obj2.addCours(obj1);
            }
            result.add(obj1);
        }
        return result;
    }
    /**
     * Selects a collection of dbObjectClassName objects pre-filled with their
     * Groupe objects.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public List<Cours> doSelectJoinGroupe(Criteria criteria)
        throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(
                    CoursPeer.DATABASE_NAME);
            List<Cours> result
                    = CoursPeer.doSelectJoinGroupe(criteria, connection);
            Transaction.commit(connection);
            connection = null;
            return result;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * Selects a collection of Cours objects pre-filled with their
     * Groupe objects.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    public List<Cours> doSelectJoinGroupe(
            Criteria criteria, Connection conn)
        throws TorqueException
    {
        setDbName(criteria);

        addSelectColumns(criteria);
        fr.iut2.sil4.data.GroupePeer.addSelectColumns(criteria);

        CompositeMapper compositeMapper = new CompositeMapper();
        compositeMapper.addMapper(
                new CoursRecordMapper(),
                0);
        compositeMapper.addMapper(
                new GroupeRecordMapper(),
                CoursPeer.numColumns);
        criteria.addJoin(CoursPeer.GROUPE_ID,
            fr.iut2.sil4.data.GroupePeer.GROUPE_ID);

        correctBooleans(criteria);

        List<Cours> result = new ArrayList<Cours>();
        List<List<Object>> rawResult = doSelect(
                criteria, compositeMapper, conn);
        for (List<Object> rawResultRow : rawResult)
        {
            Cours obj1 = (Cours) rawResultRow.get(0);
            Groupe obj2 = (Groupe) rawResultRow.get(1);

            boolean newObject = true;
            for (int j = 0; j < result.size(); j++)
            {
                Cours temp_obj1 = result.get(j);
                fr.iut2.sil4.data.Groupe temp_obj2 = temp_obj1.getGroupe();
                if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey()))
                {
                    newObject = false;
                    temp_obj2.addCours(obj1);
                    break;
                }
            }
            if (newObject)
            {
                obj2.initCourss();
                obj2.addCours(obj1);
            }
            result.add(obj1);
        }
        return result;
    }

    /**
     * selects a collection of Cours objects pre-filled with
     * all related objects.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in CoursPeerImpl.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    protected List<Cours> doSelectJoinAllExceptMatiere(Criteria criteria)
        throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(
                    CoursPeer.DATABASE_NAME);
            List<Cours> result
                    = CoursPeer.doSelectJoinAllExceptMatiere(criteria, connection);
            Transaction.commit(connection);
            connection = null;
            return result;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * selects a collection of Cours objects pre-filled with
     * related objects.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in CoursPeerImpl.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    protected List<Cours> doSelectJoinAllExceptMatiere(Criteria criteria, Connection conn)
        throws TorqueException
    {
        setDbName(criteria);

        CompositeMapper compositeMapper = new CompositeMapper();
        compositeMapper.addMapper(new CoursRecordMapper(), 0);
        CoursPeer.addSelectColumns(criteria);
        int offset = CoursPeer.numColumns;
 
        fr.iut2.sil4.data.EnseignantPeer.addSelectColumns(criteria);
        criteria.addJoin(CoursPeer.ENSEIGNANT_ID, fr.iut2.sil4.data.EnseignantPeer.ENSEIGNANT_ID);
        compositeMapper.addMapper(new EnseignantRecordMapper(), offset);
        offset = offset + fr.iut2.sil4.data.EnseignantPeer.numColumns;
 
        fr.iut2.sil4.data.GroupePeer.addSelectColumns(criteria);
        criteria.addJoin(CoursPeer.GROUPE_ID, fr.iut2.sil4.data.GroupePeer.GROUPE_ID);
        compositeMapper.addMapper(new GroupeRecordMapper(), offset);
        offset = offset + fr.iut2.sil4.data.GroupePeer.numColumns;

        correctBooleans(criteria);

        List<Cours> result = new ArrayList<Cours>();
        List<List<Object>> rawResult = doSelect(
                criteria, compositeMapper, conn);
        for (List<Object> rawResultRow : rawResult)
        {
            Cours cours = (Cours) rawResultRow.get(0);

            fr.iut2.sil4.data.Enseignant obj1 = (fr.iut2.sil4.data.Enseignant) rawResultRow.get(1);
            boolean newObject = true;
            for (int j = 0; j < result.size(); j++)
            {
                fr.iut2.sil4.data.Cours temp_cours = result.get(j);
                fr.iut2.sil4.data.Enseignant temp_obj1 = temp_cours.getEnseignant();
                if (temp_obj1.getPrimaryKey().equals(obj1.getPrimaryKey()))
                {
                    newObject = false;
                    temp_obj1.addCours(cours);
                    break;
                }
            }
            if (newObject)
            {
                obj1.initCourss();
                obj1.addCours(cours);
            }

            fr.iut2.sil4.data.Groupe obj2 = (fr.iut2.sil4.data.Groupe) rawResultRow.get(2);
            newObject = true;
            for (int j = 0; j < result.size(); j++)
            {
                fr.iut2.sil4.data.Cours temp_cours = result.get(j);
                fr.iut2.sil4.data.Groupe temp_obj2 = temp_cours.getGroupe();
                if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey()))
                {
                    newObject = false;
                    temp_obj2.addCours(cours);
                    break;
                }
            }
            if (newObject)
            {
                obj2.initCourss();
                obj2.addCours(cours);
            }
            result.add(cours);
        }
        return result;
    }
    /**
     * selects a collection of Cours objects pre-filled with
     * all related objects.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in CoursPeerImpl.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    protected List<Cours> doSelectJoinAllExceptEnseignant(Criteria criteria)
        throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(
                    CoursPeer.DATABASE_NAME);
            List<Cours> result
                    = CoursPeer.doSelectJoinAllExceptEnseignant(criteria, connection);
            Transaction.commit(connection);
            connection = null;
            return result;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * selects a collection of Cours objects pre-filled with
     * related objects.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in CoursPeerImpl.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    protected List<Cours> doSelectJoinAllExceptEnseignant(Criteria criteria, Connection conn)
        throws TorqueException
    {
        setDbName(criteria);

        CompositeMapper compositeMapper = new CompositeMapper();
        compositeMapper.addMapper(new CoursRecordMapper(), 0);
        CoursPeer.addSelectColumns(criteria);
        int offset = CoursPeer.numColumns;
 
        fr.iut2.sil4.data.MatierePeer.addSelectColumns(criteria);
        criteria.addJoin(CoursPeer.MATIERE_ID, fr.iut2.sil4.data.MatierePeer.MATIERE_ID);
        compositeMapper.addMapper(new MatiereRecordMapper(), offset);
        offset = offset + fr.iut2.sil4.data.MatierePeer.numColumns;
 
        fr.iut2.sil4.data.GroupePeer.addSelectColumns(criteria);
        criteria.addJoin(CoursPeer.GROUPE_ID, fr.iut2.sil4.data.GroupePeer.GROUPE_ID);
        compositeMapper.addMapper(new GroupeRecordMapper(), offset);
        offset = offset + fr.iut2.sil4.data.GroupePeer.numColumns;

        correctBooleans(criteria);

        List<Cours> result = new ArrayList<Cours>();
        List<List<Object>> rawResult = doSelect(
                criteria, compositeMapper, conn);
        for (List<Object> rawResultRow : rawResult)
        {
            Cours cours = (Cours) rawResultRow.get(0);

            fr.iut2.sil4.data.Matiere obj1 = (fr.iut2.sil4.data.Matiere) rawResultRow.get(1);
            boolean newObject = true;
            for (int j = 0; j < result.size(); j++)
            {
                fr.iut2.sil4.data.Cours temp_cours = result.get(j);
                fr.iut2.sil4.data.Matiere temp_obj1 = temp_cours.getMatiere();
                if (temp_obj1.getPrimaryKey().equals(obj1.getPrimaryKey()))
                {
                    newObject = false;
                    temp_obj1.addCours(cours);
                    break;
                }
            }
            if (newObject)
            {
                obj1.initCourss();
                obj1.addCours(cours);
            }

            fr.iut2.sil4.data.Groupe obj2 = (fr.iut2.sil4.data.Groupe) rawResultRow.get(2);
            newObject = true;
            for (int j = 0; j < result.size(); j++)
            {
                fr.iut2.sil4.data.Cours temp_cours = result.get(j);
                fr.iut2.sil4.data.Groupe temp_obj2 = temp_cours.getGroupe();
                if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey()))
                {
                    newObject = false;
                    temp_obj2.addCours(cours);
                    break;
                }
            }
            if (newObject)
            {
                obj2.initCourss();
                obj2.addCours(cours);
            }
            result.add(cours);
        }
        return result;
    }
    /**
     * selects a collection of Cours objects pre-filled with
     * all related objects.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in CoursPeerImpl.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    protected List<Cours> doSelectJoinAllExceptGroupe(Criteria criteria)
        throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(
                    CoursPeer.DATABASE_NAME);
            List<Cours> result
                    = CoursPeer.doSelectJoinAllExceptGroupe(criteria, connection);
            Transaction.commit(connection);
            connection = null;
            return result;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /**
     * selects a collection of Cours objects pre-filled with
     * related objects.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in CoursPeerImpl.
     *
     * @throws TorqueException Any exceptions caught during processing will be
     *         rethrown wrapped into a TorqueException.
     */
    protected List<Cours> doSelectJoinAllExceptGroupe(Criteria criteria, Connection conn)
        throws TorqueException
    {
        setDbName(criteria);

        CompositeMapper compositeMapper = new CompositeMapper();
        compositeMapper.addMapper(new CoursRecordMapper(), 0);
        CoursPeer.addSelectColumns(criteria);
        int offset = CoursPeer.numColumns;
 
        fr.iut2.sil4.data.MatierePeer.addSelectColumns(criteria);
        criteria.addJoin(CoursPeer.MATIERE_ID, fr.iut2.sil4.data.MatierePeer.MATIERE_ID);
        compositeMapper.addMapper(new MatiereRecordMapper(), offset);
        offset = offset + fr.iut2.sil4.data.MatierePeer.numColumns;
 
        fr.iut2.sil4.data.EnseignantPeer.addSelectColumns(criteria);
        criteria.addJoin(CoursPeer.ENSEIGNANT_ID, fr.iut2.sil4.data.EnseignantPeer.ENSEIGNANT_ID);
        compositeMapper.addMapper(new EnseignantRecordMapper(), offset);
        offset = offset + fr.iut2.sil4.data.EnseignantPeer.numColumns;

        correctBooleans(criteria);

        List<Cours> result = new ArrayList<Cours>();
        List<List<Object>> rawResult = doSelect(
                criteria, compositeMapper, conn);
        for (List<Object> rawResultRow : rawResult)
        {
            Cours cours = (Cours) rawResultRow.get(0);

            fr.iut2.sil4.data.Matiere obj1 = (fr.iut2.sil4.data.Matiere) rawResultRow.get(1);
            boolean newObject = true;
            for (int j = 0; j < result.size(); j++)
            {
                fr.iut2.sil4.data.Cours temp_cours = result.get(j);
                fr.iut2.sil4.data.Matiere temp_obj1 = temp_cours.getMatiere();
                if (temp_obj1.getPrimaryKey().equals(obj1.getPrimaryKey()))
                {
                    newObject = false;
                    temp_obj1.addCours(cours);
                    break;
                }
            }
            if (newObject)
            {
                obj1.initCourss();
                obj1.addCours(cours);
            }

            fr.iut2.sil4.data.Enseignant obj2 = (fr.iut2.sil4.data.Enseignant) rawResultRow.get(2);
            newObject = true;
            for (int j = 0; j < result.size(); j++)
            {
                fr.iut2.sil4.data.Cours temp_cours = result.get(j);
                fr.iut2.sil4.data.Enseignant temp_obj2 = temp_cours.getEnseignant();
                if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey()))
                {
                    newObject = false;
                    temp_obj2.addCours(cours);
                    break;
                }
            }
            if (newObject)
            {
                obj2.initCourss();
                obj2.addCours(cours);
            }
            result.add(cours);
        }
        return result;
    }

    /** 
     * Saves the passed collection as linked objects.
     * This means the following is done:
     * <ul>
     *  <li>
     *    The current collection of objects which are linked to toLinkTo
     *    and which are also in the list toSave is read from the database
     *    into the list intersection.
     *  </li>
     *  <li>
     *    All objects in toSave which are not in intersection are inserted.
     *  </li>
     *  <li>
     *    All objects in intersection are updated with the 
     *    corresponding values in toSave and saved.
     *  </li>
     *  <li>
     *    All objects in the database which are linked to toLinkTo but are 
     *    not in toSave are deleted from the database.
     *  </li>
     * </ul>
     *
     * @param toLinkTo the object which collections should be set with the
     *        values in toSave.
     * @param toSave Contains the objects to save, not null, 
     *        may not contain null.
     *
     * @throws TorqueException if an error accessing the database occurs.
     * @throws NullPointerException if toFill is null or contains null elements.
     */
    public void setAndSaveAbsences(
            Cours toLinkTo,
            Collection<Absence> toSave) 
        throws TorqueException
    {
        Connection connection = null;
        try
        {
            connection = Transaction.begin(
                    CoursPeer.DATABASE_NAME);
            setAndSaveAbsences(toLinkTo, toSave, connection);
            Transaction.commit(connection);
            connection = null;
        }
        finally
        {
            if (connection != null)
            {
                Transaction.safeRollback(connection);
            }
        }
    }

    /** 
     * Saves the passed collection as linked objects.
     * This means the following is done:
     * <ul>
     *  <li>
     *    The current collection of objects which are linked to toLinkTo
     *    and which are also in the list toSave is read from the database
     *    into the list intersection.
     *  </li>
     *  <li>
     *    All objects in toSave which are not in intersection are inserted.
     *  </li>
     *  <li>
     *    All objects in intersection are updated with the 
     *    corresponding values in toSave and saved.
     *  </li>
     *  <li>
     *    All objects in the database which are linked to toLinkTo but are 
     *    not in toSave are deleted from the database.
     *  </li>
     * </ul>
     *
     * @param toLinkTo the object which collections should be set with the
     *        values in toSave.
     * @param toSave Contains the objects to save, not null, 
     *        may not contain null.
     * @param connection the database connection to use.
     *
     * @throws TorqueException if an error accessing the database occurs.
     * @throws NullPointerException if toFill is null or contains null elements.
     */
    public void setAndSaveAbsences(
            Cours toLinkTo,
            Collection<Absence> toSave,
            Connection connection) 
        throws TorqueException
    {
        ObjectKey localKey = toLinkTo.getPrimaryKey();

        Criteria criteria = new Criteria();
        Criterion onlyReferencingCriterion = new Criterion(
                AbsencePeer.COURS_ID, 
                localKey);
        criteria.where(onlyReferencingCriterion);
        Set<ObjectKey> toSaveKeys = new HashSet<ObjectKey>();
        for (Absence toSaveElement : toSave)
        {
            ObjectKey toSaveKey = toSaveElement.getPrimaryKey();
            if (toSaveKey.getValue() != null)
            {
                toSaveKeys.add(toSaveKey);
            }
        }
        criteria.and(AbsencePeer.ABSENCE_ID, toSaveKeys, Criteria.IN);
        List<Absence> intersection
                = AbsencePeer.doSelect(criteria, connection);
        List<Absence> linkedCache;
        if (toLinkTo.isAbsencesInitialized()) {
            linkedCache = toLinkTo.getAbsences();
            linkedCache.clear();
        }
        else
        {
            linkedCache = toLinkTo.initAbsences();
        }
        for (Absence toSaveElement : toSave)
        {
            int listIndex = intersection.indexOf(toSaveElement);
            if (listIndex == -1)
            {
                toLinkTo.addAbsence(toSaveElement);
                toSaveElement.save(connection);
            }
            else
            {
                toLinkTo.addAbsence(toSaveElement);
                toSaveElement.setNew(false);
                if (!toSaveElement.valueEquals(intersection.get(listIndex)))
                {
                    //force saving if content differs
                    toSaveElement.setModified(true);
                }
                toSaveElement.save(connection);
            }
            toSaveKeys.add(toSaveElement.getPrimaryKey());
        }
        
        // delete elements not in intersection
        Criteria deleteCriteria = new Criteria();
        deleteCriteria.where(onlyReferencingCriterion);
        deleteCriteria.and(AbsencePeer.ABSENCE_ID, toSaveKeys, Criteria.NOT_IN);
        AbsencePeer.doDelete(deleteCriteria, connection);
    }


}
